"""Test utils."""

import os
import logging

from google.appengine.ext import testbed

from django.test import TestCase as _TestCase

loglevel = logging.DEBUG


class TestCase(_TestCase):
	"""Customized Django TestCase.

	This class disables the setup of Django features that are not
	available on App Engine (e.g. fixture loading). And it initializes
	the Testbed class provided by the App Engine SDK.
	"""

	def _fixture_setup(self):	# defined in django.test.TestCase
		pass

	def _fixture_teardown(self):	# defined in django.test.TestCase
		pass

	def setUp(self):
		super(TestCase, self).setUp()
		self.testbed = testbed.Testbed()
		self.testbed.activate()
		self.testbed.init_datastore_v3_stub()
		self.testbed.init_user_stub()
		self.testbed.init_urlfetch_stub()
		self.testbed.init_taskqueue_stub()

		self.hdlr = logging.StreamHandler()
		logging.getLogger().addHandler(self.hdlr)
		logging.getLogger().setLevel(loglevel)

	def tearDown(self):
		self.testbed.deactivate()
		logging.getLogger().removeHandler(self.hdlr)
		super(TestCase, self).tearDown()

	def login(self, email):
		"""Logs in a user identified by email."""
		os.environ['USER_EMAIL'] = email

	def logout(self):
		"""Logs the user out."""
		os.environ['USER_EMAIL'] = ''

